﻿using System;
using System.Collections.Generic;

namespace Runtime.Models {

    public interface IDFR_CoreModels {

        /// <summary>
        /// 账号数据
        /// </summary>
        IDFR_AccountModel Account { get; }

        /// <summary>
        /// 应用数据
        /// </summary>
        IDFR_ApplicationModel Application { get; }

        /// <summary>
        /// 网络数据
        /// </summary>
        IDFR_NetModel Net { get; }

        /// <summary>
        /// 服务器列表
        /// </summary>
        IReadOnlyListData<IDFR_ServerModel> ServerList { get; }

        /// <summary>
        /// 登录的服务器
        /// </summary>
        IDFR_ServerModel Server { get; }

        /// <summary>
        /// 特性数据
        /// </summary>
        IDFR_FeatureModel Feature { get; }

        IDFR_CoreModelsSetter AsSetter();
    }

    public interface IDFR_CoreModelsSetter {

        void AddServer(IDFR_ServerModel server);

        void SetServer(IDFR_ServerModel server);

        void ClearAllServers();
    }

    public class DFR_CoreModels : DFR_Model, IDFR_CoreModels, IDFR_CoreModelsSetter, IDisposable {

        public IDFR_CoreModelsSetter AsSetter() => this;

        [Autowired]
        public IDFR_AccountModel Account { get; private set; }

        [Autowired]
        public IDFR_ApplicationModel Application { get; private set; }

        [Autowired]
        public IDFR_NetModel Net { get; private set; }

        [Autowired]
        private readonly IListData<IDFR_ServerModel> m_ServerList;
        public IReadOnlyListData<IDFR_ServerModel> ServerList => m_ServerList;

        [Autowired]
        public IDFR_FeatureModel Feature { get; private set; }

        public IDFR_ServerModel Server { get; private set; }

        void IDFR_CoreModelsSetter.AddServer(IDFR_ServerModel server) {
            m_ServerList.Add(server);
        }

        void IDFR_CoreModelsSetter.SetServer(IDFR_ServerModel server) {
            Server = server;
        }

        void IDFR_CoreModelsSetter.ClearAllServers() {
            foreach (var server in m_ServerList) {
                if (server is IDisposable disposable) {
                    disposable.Dispose();
                }
            }
            m_ServerList.Clear();
        }

        void IDisposable.Dispose() {
            Server = null;
        }
    }
}
